package com.freshcity.page.tree
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.tree.QiYiWin;
	import com.greensock.TweenMax;
	
	import fl.controls.RadioButton;
	import fl.controls.RadioButtonGroup;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class QingYiGuo extends Component
	{
		private var _data:Array;
		private var back:QiYiWin;
		private var userGroup:RadioButtonGroup;
		
		public function QingYiGuo(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.mouseEnabled = false;
			this.visible = false;
			this.userGroup=new RadioButtonGroup("user");

			super(parent, xpos, ypos);
			back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
			back.cancelBtn.addEventListener(MouseEvent.CLICK,hide);
			back.submitBtn.addEventListener(MouseEvent.CLICK,submit);
		}
		
		protected function submit(event:MouseEvent):void
		{
			if (!this.userGroup.selection)
			{
				main.msg.showRollTipsMsg("请选择一个你想分享的人");
			}else{
				var u:Object=this.userGroup.selection.value;
				back.submitBtn.enabled = false;
				main.commu.send(OpCodes.OPEN_QINGYI, u, qiyiCallback);
			}
		}
		
		private function qiyiCallback(data:*):void
		{
			main.msg.showPopUpMsg("神奇的情意果已产生了互动任务，赶快联系TA吧",MsgManager.HAHA,0,MsgManager.CENTER);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new QiYiWin();
			addChild(back);
			
			back.fruits.gotoAndStop(2);
		}
		
		override public function destroy():void
		{
			EffectManager.hideUIMask();
			back.closeBtn.removeEventListener(MouseEvent.CLICK,hide);
			back.cancelBtn.removeEventListener(MouseEvent.CLICK,hide);
			back.submitBtn.removeEventListener(MouseEvent.CLICK,submit);
			super.destroy();
		}
		
		override public function hide(event:Event=null):void
		{
			TweenMax.to(this,0.3,{autoAlpha:0,y:"+30",onComplete:destroy});
		}
		
		override public function show(event:Event=null):void
		{
			if(!this.parent){
				LayerManager.popupLayer.addChild(this);
			}
			this.x = (stage.stageWidth-back.width)/2;
			this.y = (stage.stageHeight-back.height)/2 - 30;
			
			TweenMax.to(this,0.3,{autoAlpha:1,y:"+30"});
		}

		public function get data():Array
		{
			return _data;
		}

		public function set data(value:Array):void
		{
			_data = value;
			var vArray:Array;
			var spacing:int = 5;
			var currXpos:int = 13;
			
			for each (var s:String in Array) 
			{
				var rb:RadioButton = new RadioButton();
				rb.group = this.userGroup;
				vArray = s.split(",");
				rb.label = "用户"+vArray[0]+"("+vArray[0]+")";
				rb.value = vArray[0];
				rb.move(currXpos,68);
				back.addChild(rb);
				
				currXpos += (rb.width+spacing);
			}
		}
	}
}